<?php
class CitiesController extends AppController {

	var $name = 'Cities';
	
	public function beforeFilter() {
            parent::beforeFilter();
            $this->Auth->allow('changeCity', 'changeYear', 'changeQuarter');
        }
	
	function changeCity()
	{
		if (!empty($this->data)) {
			$city = $this->City->find('first', array('conditions'=>array('City.id'=>$this->data["city_id"])));
			$this->Session->write("c_city_id",  $city["City"]["id"]);
			$this->redirect("http://".$_SERVER['SERVER_NAME'].$this->data["redirect_url"]);
			//var_dump($this->params);
		}
	}
	
	function changeYear()
	{
		if (!empty($this->data)) {			
			$this->Session->write("c_year",  $this->data["change_year"]);
			$this->redirect("http://".$_SERVER['SERVER_NAME'].$this->data["redirect_url"]);
			//var_dump($this->params);
		}
	}
	
	function changeQuarter()
	{
		if (!empty($this->data)) {			
			$this->Session->write("c_quarter",  $this->data["change_quarter"]);
			$this->redirect("http://".$_SERVER['SERVER_NAME'].$this->data["redirect_url"]);
			//var_dump($this->params);
		}
	}
	
	function map()
	{
		$this->layout = "home";
	}
	
	function details($id = null)
	{
		$this->layout = "home";
		//echo $this->Session->read("c_city_id");
		//echo $id;
		if (!$id) {
			if($this->Session->read("c_city_id") != '')
			{
				$id = $this->Session->read("c_city_id");
			}
			else
			{
				$this->Session->setFlash(__('Invalid city', true));
				$this->redirect(array('action' => 'details'));
			}			
		}		
		//echo $id."dssfs";
		$city = $this->City->read(null, $id);
		//$this->Session->write("c_city_id",  $city["City"]["id"]);
		
		$pop_array = array();
		$outlay_array = array();
		$year_array = array();
		foreach($city["CityDetail"] as $key => $c)
		{
			$pop_array[] = $c["population"];
			$outlay_array[] = '['.$c["year"].', '.($c["outlay"]/1000000).']';
			$year_array[] = $c["year"];
		}
		//var_dump($pop_array);
		$pop_string = "[".implode(', ', $pop_array)."]";
		$outlay_string = "[[".implode(', ', $outlay_array)."]]";
		$year_string = "[".implode(', ', $year_array)."]";
		
		//get select year details
		$details = $this->City->CityDetail->find('first', array('conditions'=>array('CityDetail.city_id'=>$id, 'CityDetail.year'=>$this->Session->read("c_year"))));
		$details["CityDetail"]["population"] = isset($details["CityDetail"]["population"]) ? number_format($details["CityDetail"]["population"],0,".", ",") : 0;
		$details["CityDetail"]["area"] = isset($details["CityDetail"]["area"]) && $details["CityDetail"]["area"] != '' ? number_format($details["CityDetail"]["area"],0,".", ",") : 0;
		
		
		$count = array();
		//Nhan luc
		Controller::loadModel('HumanResource');
		$c_e = $this->HumanResource->find('all', array('fields' => array('sum(HumanResource.value) AS total'),
								'conditions' => array(
										'HumanResource.year'=>$this->Session->read("c_year"),
										'HumanResource.city_id'=>$id
									)
							));
		//var_dump($c_e[0][0]["total"]);
		$count["employees"] = number_format($c_e[0][0]["total"],0,".", ",");
		
		//devices sum
		Controller::loadModel('DeviceResource');
		$c_d = $this->DeviceResource->find('all', array('fields' => array('sum(DeviceResource.value) AS total'),
								'conditions' => array(
										'DeviceResource.year'=>$this->Session->read("c_year"),
										'DeviceResource.city_id'=>$id
									)
							));
		//var_dump($c_d[0][0]["total"]);
		$count["devices"] = number_format($c_d[0][0]["total"],0,".", ",");
		
		//devices sum
		Controller::loadModel('TreatmentRecord');
		$c_t = $this->TreatmentRecord->find('all', array('fields' => array('sum(TreatmentRecord.value) AS total'),
								'conditions' => array(
										'TreatmentRecord.year'=>$this->Session->read("c_year"),
										'TreatmentRecord.city_id'=>$id
									)
							));
		//var_dump($c_d[0][0]["total"]);
		$count["treatments"] = number_format($c_t[0][0]["total"],0,".", ",");
		
		//devices sum
		Controller::loadModel('ScienceProject');
		$count["science_projects"] = $this->ScienceProject->find('count', array(
								'conditions' => array(
										'ScienceProject.year'=>$this->Session->read("c_year"),
										'ScienceProject.city_id'=>$id
									)
							));
		//var_dump($c_sp);
		//$count["treatments"] = number_format($c_t[0][0]["total"],0,".", ",");
		
		$this->set('city', $city);
		$this->set('count', $count);
		$this->set('details', $details);
		$this->set('pop_string', $pop_string);
		$this->set('outlay_string', $outlay_string);
		$this->set('year_string', $year_string);
	}
	
	function admin_index() {
		$this->City->recursive = 0;
		$this->set('cities', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid city', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('city', $this->City->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->City->create();
			if ($this->City->save($this->data)) {
				$this->Session->setFlash(__('The city has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The city could not be saved. Please, try again.', true));
			}
		}
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid city', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->City->save($this->data)) {
				$this->Session->setFlash(__('The city has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The city could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->City->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for city', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->City->delete($id)) {
			$this->Session->setFlash(__('City deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('City was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
